//
//  ShellRateView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/9/27.
//  Copyright © 2019年 VV. All rights reserved.
//

#import "ShellRateView.h"
#import "ShellRateHeadView.h"
#import "ShellRateCell.h"
#import "ShellRateViewModel.h"
#import "LZBaseTableView.h"


@interface  ShellRateView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) ShellRateViewModel *viewModel;

@property (nonatomic,strong) LZBaseTableView *tableView;


@end

@implementation ShellRateView

- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        
        self.viewModel = (ShellRateViewModel *)viewModel;
        [self setupView];
    }
    return self;
}


#pragma mark - setupView
- (void)setupView{
    
    ShellRateHeadView *headView = [[ShellRateHeadView alloc] init];
    headView.frame = CGRectMake(0, 0, K_SCREENWIDTH, 84);
    [self addSubview:headView];

    
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(84);
        make.left.right.bottom.mas_equalTo(0);
    }];
    
    @weakify(self);
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self refreshUI];
        
    }];
    
    //获取实时汇率
    [self.viewModel getExchangeRates:YES];
    [self refreshUI];

}



//刷新UI
- (void)refreshUI{
    [self.tableView reloadData];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShellRateCell *rateCell = [tableView dequeueReusableCellWithIdentifier:@"ShellRateCell"];
    rateCell.indexPath = indexPath;
    rateCell.modelObject = self.viewModel.dataArray[indexPath.row];
    return rateCell;
}


#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark - get && set
- (LZBaseTableView *)tableView{
    if(!_tableView){
        _tableView = [[LZBaseTableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = COLOR_BACKGROUND;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ShellRateCell class] forCellReuseIdentifier:@"ShellRateCell"];
        _tableView.rowHeight = 50;
    }
    return _tableView;
}




@end
